***********************************
* SOME DESCRIPTIVES OF THE DATA *
***********************************


///SET DIRECTORY/log-files/set more off

clear all
cd "C:\Userdata\Shared"
set more off

*capture log close
*log using "Logs\Ethnic Enclaves, political participation\log_descriptives", replace


use "E:\ProjData\Ethnic Enclaves, political participation\use\ind_panel"



///PREPARE CONTROL VARIABLES
*age
label variable age "Age"
gen age2 = age^2
label variable age2  "Age$^2$"

*sex
label variable sex "Men"

*Married: 
gen married = 1 if Civil==2 | Civil ==3 | Civil ==7
replace married =0 if married ==.

label variable married "Married"

///Children or not
gen parent =1 if Barn0_3>0 | Barn4_6>0 | Barn7_10>0 | Barn11_15> 0 | Barn16_17>0
replace parent =0 if parent==.
replace parent =. if Barn0_3==.
label variable parent "Parent"

///Number of kids
gen children = Barn0_3 + Barn4_6 + Barn7_10 + Barn11_15 + Barn16_17
label variable children  "$\#$ of children"

///Education
encode Sun2000niva_old, gen(Sun2000niva_old_dstr)
gen high_educ = 1 if Sun2000niva_old_dstr>4
replace high_educ =0 if high_educ==.
replace high_educ=. if Sun2000niva_old_dstr==.

label variable high_educ  "Highly educated"


///global macro for covariates in all regressions
global cov sex age age2 married parent children high_educ


///INDEPENDET VARIABLE (ENCLAVE VARIABLES): coethnics
*number of coethnics
*drop the person him/herself
replace coethnics = coethnics-1
label variable coethnics  "$\#$ Coethnics"
egen stdcoethnics = std(coethnics)
label variable stdcoethnics "std number of coethnics \\ (mean =0, std=1)"

*share of population
gen coshare = coethnics/pop
label variable coshare "Coethnics/parish pop"
egen stdcoshare = std(coshare)
label variable stdcoshare  "Coethnics/ \\parish inhabitants"


///voted coethnics as share of right to vote 1982
gen voted_coethnics = voted/right
replace voted_coethnics=0 if voted_coethnics==.
egen stdvoted_coethnics = std(voted_coethnics)
label variable voted_coethnics "Share 1982 voters among coethnics \\ with right to vote 82"
label variable stdvoted_coethnics "Std share 1982 voters among coethnics \\ with right to vote 82"

///voted 1982 as share of coethnics
gen voted_coethnics2 = voted/coethnics
egen stdvoted_coethnics2 = std(voted_coethnics2)
label variable voted_coethnics2 "Share 1982 voters among all coethnics"
label variable stdvoted_coethnics "Std share 1982 voters \\among all coethnics"

*voted 1982 as share of population
gen sharevoted = voted/pop
egen stdsharevoted = std(sharevoted)
label variable stdsharevoted  "coethnics voting in 1982 \\election/parish inhabitants"


////OUTCOME VARIABLE!

///voted or not
gen voted_k =0 if k==1
replace voted_k=1 if voted_k==.

label variable voted_k  "Voted in local election"

gen voted_r = 0 if r==1
replace voted_r=1 if r==2  | r==4 | r==5
replace voted_r=. if r==3

label variable voted_r  "Voted in national election"

*Labels for different types of voting
replace k = 3 if k==4 | k==5

lab def k 1 "no vote",
lab def k 2 "voted: at polling station", add
lab def k 3 "voted: using post", add

lab values k k	

tab k, sort


///OTHER CONTROLS:
///moving
*Moving variables: 
replace move_out_muni=0 if move_out_muni==.
replace move=0 if move==.
label variable move_out_muni "moved out of municipality arrival year"


****************
* DESCRIPTIONS *
****************


///Frquency of birth countries
bysort year: tab IFAUkod, sort

*Output
eststo clear
eststo: estpost tab IFAUkod if year==1987
eststo: estpost tab IFAUkod if year==1988
eststo: estpost tab IFAUkod if year==1989
eststo: estpost tab IFAUkod if year==1990
eststo: estpost tab IFAUkod if year==1991

esttab using "C:\Users\henrik\Dropbox\Ethnic Enclaves and Voting\Output\describe\frequency_ethnicity.tex", ///
replace fragment nomtitles nonotes nolines compress label nonumbers nogaps 


///SUMMARY STATS

///first basic
eststo clear
*an overall summary stats table
eststo: estpost tabstat voted_k coethnics coshare voted_coethnics2 ///
sex age married parent children high_educ move_out_muni , ///
statistics(mean sd min max n) columns(statistics) 

///NO FLOAT
esttab using "C:\Users\henrik\Dropbox\Ethnic Enclaves and Voting\Output\describe\sumstat.tex", ///
nonumbers fragment noobs label cells("mean(fmt(2)) sd min max count(fmt(0))") ///
refcat(voted_k " &  &  & &  & \\ \textbf{Outcome}" coethnics " &  &  & &  & \\ \textbf{Enclave information}" sex  " &  &  & &  & \\ \textbf{Controls}", nolabel) replace 


*create a variable ==1 for larger than median treatment variable
cumul(coshare), gen(cumcoshare)
label variable cumcoshare "cumulative function of coethnics as share of parish population"
gen largeenclave = 1 if cumcoshare>0.5      /*Median*/
replace largeenclave=0 if largeenclave==.

lab def largeenclave 1 "Large Enclave (>median)"
lab def largeenclave 0 "Small Enclave (<median)", add
lab values largeenclave largeenclave	

lab def voted_k 1 "Voted"
lab def voted_k 0 "Not voted", add
lab values voted_k voted_k	

*Create table
eststo clear
*an overall summary stats table
eststo: estpost tabstat voted_k coethnics coshare voted_coethnics2 ///
sex age married parent children high_educ move_out_muni , by(largeenclave) ///
statistics(mean sd) columns(statistics) listwise

///NO FLOAT
esttab using "C:\Users\henrik\Dropbox\Ethnic Enclaves and Voting\Output\describe\sumstat_byen.tex", ///
main(mean) aux(sd) nostar unstack noobs nonote label ///
refcat(voted_k " &  &  & &  & \\ \textbf{Outcome}" coethnics " &  &  & &  & \\ \textbf{Enclave information}" sex  " &  &  & &  & \\ \textbf{Controls}", nolabel) replace 


////for the long term


rename r r_first
rename k k_first
rename l l_first
 
 
merge 1:m LopNr using "D:\SCB_ConPol\Stata\VD\Valdelt_2010", keepusing (LopNr r k l)
keep if _merge==3


*check for duplicates - OK
*duplicates report LopNr 

///create voting variable

///voted or not
gen voted_k2010 =0 if k==1
replace voted_k2010=1 if voted_k2010==.

label variable voted_k2010  "voted in local election 2010"


///summary of voted 2010
eststo clear
eststo: estpost tabstat voted_k2010 , ///
statistics(mean sd min max n) columns(statistics) 
esttab using "C:\Users\henrik\Dropbox\Ethnic Enclaves and Voting\Output\describe\sumstat2010.tex", ///
nonumbers fragment noobs label cells("mean(fmt(2)) sd min max count(fmt(0))") ///
refcat(voted_k " &  &  & &  & \\ \textbf{Outcome}" coethnics " &  &  & &  & \\ \textbf{Enclave information}" sex  " &  &  & &  & \\ \textbf{Controls}", nolabel) replace 

*an overall summary stats table
eststo clear
eststo: estpost tabstat voted_k2010 , by(largeenclave) ///
statistics(mean sd) columns(statistics) listwise

///NO FLOAT
esttab using "C:\Users\henrik\Dropbox\Ethnic Enclaves and Voting\Output\describe\sumstat_byen2010.tex", ///
main(mean) aux(sd) nostar unstack noobs nonote label ///
refcat(voted_k " &  &  & &  & \\ \textbf{Outcome}" coethnics " &  &  & &  & \\ \textbf{Enclave information}" sex  " &  &  & &  & \\ \textbf{Controls}", nolabel) replace 




*log close
